//
//  AreaDetailViewController.m
//  MonitoringNetwork
//
//  Created by admin on 2017/12/4.
//  Copyright © 2017年 wtmcxlm. All rights reserved.
//

#import "AreaDetailViewController.h"
#import "AttributionAreaCell.h"
@interface AreaDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *areadetailTableView;
@property (nonatomic,strong)UILabel *totalCarLabel;
@property (nonatomic,strong)UILabel *totalMileageLabel;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)LoadNetView *loadNetView;
@end
static NSString *AreaAttributionID = @"AreaAttributionCell";
@implementation AreaDetailViewController
- (LoadNetView *)loadNetView
{
    if (!_loadNetView) {
        _loadNetView = [LoadNetView new];
        [self.view addSubview:_loadNetView];
        [_loadNetView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.centerY.mas_equalTo(self.view);
        }];
        _loadNetView.hidden = NO;
    }
    return _loadNetView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupNavigationTitleWithTitle:_titleStr];
    [self loadUI];
    [self loadData];
    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipemethod:)];
    swipe.direction = UISwipeGestureRecognizerDirectionRight;
    [self.view addGestureRecognizer:swipe];
}
- (void)swipemethod:(UISwipeGestureRecognizer *)swipe
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)loadData
{
    NSInteger areaId = [self.areaRegionId integerValue];
    NSDictionary *param = @{
                            @"areaRegionId":@(areaId)
                            };
    [self.loadNetView start];
    [[NetWorkTool sharedNetworkTool] requestWithUrlString:kURL_AreaStat method:@"POST" parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        NSLog(@"-----%@-----",uploadProgress);
    } success:^(id  _Nullable responseObject) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeSuccess];
        NSLog(@"%@",responseObject);
        if ([responseObject[@"state"] integerValue] == 0) {
            [self setBottomLabelWithDictionary:responseObject];
            NSArray *areaRegionsArr = responseObject[@"obj"][@"areas"];
            _dataArray = [NSMutableArray array];
            if (areaRegionsArr.count && areaRegionsArr) {
                for (NSDictionary *dic in areaRegionsArr) {
                    AttrAreaModel *model = [AttrAreaModel new];
                    [model yy_modelSetWithDictionary:dic];
                    [_dataArray addObject:model];
                }
            }
            if (_dataArray.count) {
                [_areadetailTableView reloadData];
            }
        }else{
            
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self.loadNetView endAnimationWithResult:LoadingViewResultTypeError];
    }];
}

- (void)loadUI
{
    //设置导航栏左右按钮
    [self setLeftBarButtonItemWithNormalImageName:@"backNormal" andSelectImageName:@"backSel"];
    
    UIView *headerView = [UIView new];
    headerView.backgroundColor = [UIColor colorWithRed:0.96f green:0.96f blue:0.96f alpha:1.00f];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(NavationBarHeight);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(kHeightOfPt(116));
    }];
    
    UILabel *areaLabel = [UILabel new];
    areaLabel.textAlignment = NSTextAlignmentCenter;
    areaLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    areaLabel.text = @"归属地区";
    [headerView addSubview:areaLabel];
    [areaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView);
    }];
    
    UILabel *carNumLabel = [UILabel new];
    carNumLabel.textAlignment = NSTextAlignmentCenter;
    carNumLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    carNumLabel.text = @"车辆数";
    [headerView addSubview:carNumLabel];
    [carNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView).offset(kScreenWidth*0.25);
    }];
    
    UILabel *yesterdayLabel = [UILabel new];
    yesterdayLabel.textAlignment = NSTextAlignmentCenter;
    yesterdayLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    yesterdayLabel.text = @"昨日里程";
    [headerView addSubview:yesterdayLabel];
    [yesterdayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView).offset(kScreenWidth*0.5);
    }];
    
    UILabel *todayLabel = [UILabel new];
    todayLabel.textAlignment = NSTextAlignmentCenter;
    todayLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    todayLabel.text = @"今日里程";
    [headerView addSubview:todayLabel];
    [todayLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headerView);
        make.width.mas_equalTo(kScreenWidth*0.25);
        make.left.mas_equalTo(headerView).offset(kScreenWidth*0.75);
    }];
    
    UIView *bottomView = [UIView new];
    bottomView.backgroundColor = [UIColor colorWithRed:0.99f green:0.91f blue:0.91f alpha:1.00f];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(kHeightOfPt(100));
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
    }];
    
    self.totalCarLabel = [UILabel new];
    self.totalCarLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    self.totalCarLabel.textAlignment = NSTextAlignmentCenter;
    self.totalCarLabel.text = @"车辆总数:0";
    [bottomView addSubview:self.totalCarLabel];
    [self.totalCarLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(bottomView.mas_left);
        make.width.mas_equalTo(kScreenWidth*0.5);
        make.centerY.mas_equalTo(bottomView);
    }];
    
    self.totalMileageLabel = [UILabel new];
    self.totalMileageLabel.font = [UIFont systemFontOfSize:NormalFontSize];
    self.totalMileageLabel.textAlignment = NSTextAlignmentCenter;
    self.totalMileageLabel.text = @"今日里程数:0";
    [bottomView addSubview:self.totalMileageLabel];
    [self.totalMileageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(bottomView.mas_right);
        make.width.mas_equalTo(kScreenWidth*0.5);
        make.centerY.mas_equalTo(bottomView);
    }];
    
    self.areadetailTableView = [UITableView new];
    [self.areadetailTableView registerClass:[AttributionAreaCell class] forCellReuseIdentifier:AreaAttributionID];
    self.areadetailTableView.delegate = self;
    self.areadetailTableView.dataSource = self;
    self.areadetailTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.areadetailTableView];
    [self.areadetailTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headerView.mas_bottom);
        make.left.right.bottom.mas_equalTo(self.view);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-TabBarHeight-NavationBarHeight-kHeightOfPt(100));
    }];
    
}
- (void)leftItemBtnClick:(UIButton *)sender
{
    [super leftItemBtnClick:sender];
    sender.enabled = NO;
    [self.navigationController popViewControllerAnimated:YES];
    sender.enabled = YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableView 代理 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AttributionAreaCell *attributionCell  = [tableView dequeueReusableCellWithIdentifier:AreaAttributionID];
    if (_dataArray.count) {
        attributionCell.model = _dataArray[indexPath.row];
    }
    attributionCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return attributionCell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 59;
}

- (void)setBottomLabelWithDictionary:(NSDictionary *)dic
{
    NSString *carString = dic[@"obj"][@"carNum"];
    NSString *mileString = dic[@"obj"][@"todayMileage"];
    self.totalCarLabel.text = [NSString stringWithFormat:@"车辆总数:%@",carString];
    self.totalMileageLabel.text = [NSString stringWithFormat:@"今日里程数:%.2lfkm",[mileString doubleValue]];
}
@end
